package com.atguigu.gmall.product.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


//ServiceImpl 注入BaseAttrInfoMapper和BaseAttrInfo
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {


    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 根据分类Id 获取平台属性集合
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */

    @Override
    public List<BaseAttrInfo> getattrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //获取持久层对象调用持久层自定义查询方法
        BaseAttrInfoMapper baseAttrInfoMapper = this.getBaseMapper();
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.getAttrInfoList(category1Id, category2Id, category3Id);
        return baseAttrInfoList;
    }

    /**
     * 保存/修改平台属性   和    平台  属性值
     *
     * @param baseAttrInfo
     * @return
     */
    @Override
    //发生Exception异常就全部回滚
    @Transactional(rollbackFor = Exception.class)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1判断是否有平台属性id提交 提交:修改操作. 没提交:新增操作
        if (baseAttrInfo.getId() == null) {
            this.save(baseAttrInfo);
            // 2 新增操作
            //新增平台属性记录到平台属性表base_attr_info
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存方法
                baseAttrValueService.saveBatch(attrValueList);
            }
            //新增平台属性值记录到平台属性值表base_attr_value

        } else {
            //3修改
            this.updateById(baseAttrInfo);
            //先删除平台属性值再新增
            LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAttrValue::getAttrId, baseAttrInfo.getId());
            baseAttrValueService.remove(queryWrapper);

            //新增平台属性记录到平台属性表base_attr_info
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存方法
                baseAttrValueService.saveBatch(attrValueList);
            }
        }

    }

    /**
     * 根据平台属性Id 获取到平台属性值集合
     *
     * @param attrId
     * @return
     */
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAttrValue::getAttrId, attrId);
        return baseAttrValueService.list(queryWrapper);
    }

    /**
     * 根据SkuID查询当前商品包含平台属性以及属性值
     *
     * @param skuId
     * @return
     */
    @Override
    @GmallCache(prefix = "attrList:")
    public List<BaseAttrInfo> getAttrList(Long skuId) {
        //获取平台属性值持久层对象 调用自定义sql   //获取持久层对象调用持久层自定义查询方法
        BaseAttrInfoMapper baseAttrInfoMapper = this.getBaseMapper();
        List<BaseAttrInfo> list = baseAttrInfoMapper.getAttrListBySkuId(skuId);
        //给AttrValue赋attrValueList里的valueName
        list.stream().forEach(baseAttrInfo -> {
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                //只要sku选一个就有值
                baseAttrInfo.setAttrValue(attrValueList.get(0).getValueName());
            }
        });
        return list;
    }
}
